// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 后台全局JS函数 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------


/**
 * 重新设置label
 *
 * @param $form
 */
g.resetFormLabel = function ($form) {
    if ($(document.body).height == 0 || $(document.body).width == 0) {
        setTimeout(function () {
            g.resetFormLabel($form);
        }, 500);
        return;
    }
    var labelMaxWidth = 0;
    $form.find(".layui-form-label").css("width", "auto");
    $form.find(".layui-form-label").each(function () {
        if ($(this).width() > labelMaxWidth) {
            labelMaxWidth = $(this).width();
        }
        $(this).html($(this).text().replace("(*)", "<span style='color: red;'>(*)</span>"));
    });
    if (labelMaxWidth < 80) {
        labelMaxWidth = 80;
    }
    $form.find(".layui-form-label").width(labelMaxWidth + 10);
    $form.find(".layui-input-block").css("margin-left", (labelMaxWidth + 40) + "px");
};

/**
 * 重新使得iframe高度自适应
 *
 * @param iframe
 */
g.resetIframeHeight = function (iframe) {
    var height = $(iframe.contentDocument.body).height();
    if (height == 0) {
        setTimeout(function () {
            g.resetIframeHeight(iframe);
        }, 500);
        return;
    }
    var deltaHeight = $(iframe).attr("data-delta-height");
    if (deltaHeight) {
        height += parseInt(deltaHeight);
    }
    $(iframe).height(height);
};

/**
 * 关闭当前页面所在的iframe弹窗
 */
g.tryCloseIframeDialog = function () {
    if (!window.frameElement || !window.parent) {
        return;
    }
    var id = window.frameElement.id;
    if (!id.indexOf("layui-layer-iframe") === 0) {
        return;
    }
    var idx = id.replace("layui-layer-iframe", "");
    window.parent.layer.close(idx);
};

$(document).ready(function () {
    /**
     * iframe高度自适应
     */
    $("iframe.auto-height-iframe").each(function () {
        var iframe = this;
        if (iframe.attachEvent) {
            iframe.attachEvent("onload", function () {
                g.resetIframeHeight(iframe);
                setTimeout(function () {
                    g.resetIframeHeight(iframe);
                }, 500);
            });
        } else {
            iframe.onload = function () {
                g.resetIframeHeight(iframe);
                setTimeout(function () {
                    g.resetIframeHeight(iframe);
                }, 500);
            };
        }
    });

    /**
     * form中的label自适应
     */
    $(".layui-form.auto-process-label").each(function () {
        g.resetFormLabel($(this));
    });

    //回车、esc关闭对话框
    $(window).keyup(function () {
        var key = event.keyCode || event.which;
        if (key == 27 || key == 13) {
            layer.closeAll('dialog');
        }
    });
});
